package com.example.studentalarm.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDexApplication;

import com.example.studentalarm.utils.LogUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/12/28.
 */

public class BaseApplication   extends MultiDexApplication {
    private static BaseApplication instance;
    private Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        String curProcess = getProcessName(this, android.os.Process.myPid());
        if (!getPackageName().equals(curProcess)) {
            return;
        }
        init();
    }

    /*
   * 防止创建进程重新初始化了
   * **/
    private String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps != null && !runningApps.isEmpty()) {
            for (ActivityManager.RunningAppProcessInfo processInfo: runningApps) {
                if (processInfo.pid == pid) {
                    return processInfo.processName;
                }
            }
        }
        return null;
    }

    private void init() {
        instance = this;
        mContext = getApplicationContext();
        LogUtil.setDEBUG(false);//关闭日志
    }



    public static BaseApplication getInstance() {
        if (instance == null) {
            synchronized (BaseApplication.class) {
                if (instance == null) {
                    instance = new BaseApplication();
                }
            }
        }
        return instance;
    }

}


